<!-- 弹窗 详情 -->
<script lang="ts" setup>
import type { FormInstance, FormRules } from "element-plus";

import { EQP_VALIDITY_OPTIONS } from "@/config/enum-map";
import { whManagerBsTbFireEqpSaveOrUpdateUsingPost } from "@/genapi/wh";
import { useAsync } from "@/hooks/use-async";
import { useCompanyStore } from "@/stores/company";

import { injectState } from "../state";

defineOptions({ name: "DetailDialog" });

const companyStore = useCompanyStore();
const { page } = injectState()!;

const dialogRef = shallowRef<InstanceType<GlobalComponents["PromiseDialog"]>>();
const formRef = shallowRef<FormInstance>();

const dialogType = ref<"add" | "edit" | "detail" | string>();

const titleMap: Record<string, string> = {
	add: "新建消防设施",
	edit: "编辑消防设施",
	detail: "消防设施详情",
};

const beforeClose = async () => {
	if (dialogType.value !== "detail") {
		await ElMessageBox.confirm("确认关闭？");
	}
};

const trigger = async (type = "add", value = {} as any) => {
	dialogType.value = type;
	model.value = JSON.parse(JSON.stringify(value)) as any;
	await dialogRef.value?.open();
};

defineExpose({
	trigger,
});

const model = ref<Record<string, any>>({});

const rules: FormRules<typeof model.value> = {
	name: { required: true, message: "请输入消防设备名称", trigger: "blur" },
	specifications: {
		required: true,
		message: "请输入设备规格",
		trigger: "blur",
	},
	eqpValidity: { required: true, message: "请选择设备有效性", trigger: "blur" },
};

const { execute, isLoading } = useAsync(async () => {
	await formRef.value?.validate();
	await ElMessageBox.confirm("确认提交？", "提示", {
		type: "warning",
	});
	await whManagerBsTbFireEqpSaveOrUpdateUsingPost({
		data: { ...model.value, enterpriseId: companyStore.currentCompanyId },
	});
	page.execute();
	ElMessage.success("提交成功");
	dialogRef.value?.complete();
});
</script>

<template>
	<PromiseDialog
		ref="dialogRef"
		:title="titleMap[dialogType!]"
		:before-close="beforeClose"
		max-h="884px!"
		w="707px!"
	>
		<el-scrollbar>
			<el-form
				ref="formRef"
				:disabled="dialogType === 'detail'"
				:model="model"
				:rules="rules"
				inline
				label-position="top"
				class="gap-x-55px grid! grid-cols-2!"
			>
				<el-form-item label="消防设备名称" prop="name">
					<el-input
						v-model="model['name']"
						clearable
						placeholder="请输入消防设备名称"
					/>
				</el-form-item>
				<el-form-item label="设备有效性" prop="eqpValidity">
					<el-select-v2
						v-model="model['eqpValidity']"
						:options="EQP_VALIDITY_OPTIONS"
						clearable
						placeholder="请选择设备有效性"
					/>
				</el-form-item>
				<el-form-item label="设备规格" prop="specifications">
					<el-input
						v-model="model['specifications']"
						clearable
						placeholder="请输入设备规格"
					/>
				</el-form-item>
				<el-form-item label="设备数量" prop="eqpNum">
					<el-input
						v-model="model['eqpNum']"
						clearable
						placeholder="请输入设备数量"
					/>
				</el-form-item>
				<el-form-item label="设备型号" prop="eqpType">
					<el-input
						v-model="model['eqpType']"
						clearable
						placeholder="请输入设备型号"
					/>
				</el-form-item>
				<el-form-item label="出厂日期" prop="factoryDate">
					<el-date-picker
						v-model="model['factoryDate']"
						class="w-100%!"
						value-format="YYYY-MM-DD"
						clearable
						placeholder="请输入出厂日期"
					/>
				</el-form-item>
				<el-form-item label="安装日期" prop="installDate">
					<el-date-picker
						v-model="model['installDate']"
						class="w-100%!"
						value-format="YYYY-MM-DD"
						clearable
						placeholder="请输入安装日期"
					/>
				</el-form-item>
				<el-form-item label="供应商" prop="supplier">
					<el-input
						v-model="model['supplier']"
						clearable
						placeholder="请输入供应商"
					/>
				</el-form-item>
				<el-form-item label="位置" prop="eqpPath" grid="col-span-2!">
					<el-input
						v-model="model['eqpPath']"
						clearable
						placeholder="请输入位置"
					/>
				</el-form-item>
			</el-form>
		</el-scrollbar>
		<template v-if="dialogType !== 'detail'" #footer>
			<el-button @click="dialogRef?.close()">取消</el-button>
			<el-button type="primary" :is-loading="isLoading" @click="execute()"
				>确认</el-button
			>
		</template>
	</PromiseDialog>
</template>
